package ynu.edu.Service.Impl;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import ynu.edu.Dao.CarbonDao;
import ynu.edu.Dao.DeviceDao;
import ynu.edu.Entity.Carbon;
import ynu.edu.Entity.Device;
import ynu.edu.Service.ISensorService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class CarbonService implements ISensorService {
    @Resource
    DeviceDao deviceDao;
    @Resource
    CarbonDao carbonDao;
    @Override
    public void insert(Double value){
        Carbon carbon = new Carbon();
        Device device = deviceDao.getReferenceById(5L);
        carbon.setDevice(device);
        carbon.setValue(value);

        carbonDao.save(carbon);
    }

    @Override
    public Double getValue(){
        Carbon carbon = carbonDao.findFirstByOrderByCreateTimeDesc().get();
        return carbon.getValue();
    }
    @Override
    public List<Double> getValuesFromLastDay() {
        LocalDateTime oneDayAgo = LocalDateTime.now().minusDays(1);
        List<Double> values = new ArrayList<>();
        List<Carbon> carbons = carbonDao.findByCreateTimeAfter(oneDayAgo);
        for (Carbon carbon : carbons){
            values.add(carbon.getValue());
        }
        return values;
    }
    @Override
    public List<Date> getTimeFromLastDay(){
        LocalDateTime oneDayAgo = LocalDateTime.now().minusDays(1);
        List<Date> times = new ArrayList<>();
        List<Carbon> carbons = carbonDao.findByCreateTimeAfter(oneDayAgo);
        for (Carbon carbon : carbons){
            times.add(carbon.getCreateTime());
        }
        return times;
    }
}
